﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zero.Code;
using Zero.BLL;
using Zero.Model;
using System.Text;
using Newtonsoft.Json;

namespace Zero.Framework.Controllers.Home
{
    
    public class ClientsDataController : Controller
    {

        /// <summary>
        /// 获取菜单集合
        /// </summary>
        /// <returns></returns>
        [HandlerAjaxOnly]
        [HttpPost]
        public string GetMenuList()
        {
            
            var roleId = OperatorProvider.Provider.GetCurrent().RoleId;//获取当前用户角色ID
            //List<modular> ModularList=new BLL.RoleAuthorization().GetModularsList(roleId);
            //return JsonConvert.SerializeObject(ModularList);
            return ToMenuJson(new BLL.RoleAuthorization().GetModularsList(roleId), 0);
        }

        private string ToMenuJson(List<modular> data, int parentId)
        {
            StringBuilder sbJson = new StringBuilder();
            sbJson.Append("[");
            List<modular> entitys = data.FindAll(t => t.M_ParentId == parentId);
            if (entitys.Count > 0)
            {
                foreach (var item in entitys)
                {
                    string strJson = item.ToJson();
                    strJson = strJson.Insert(strJson.Length - 1, ",\"ChildNodes\":" + ToMenuJson(data, item.M_Id) + "");
                    sbJson.Append(strJson + ",");
                }
                sbJson = sbJson.Remove(sbJson.Length - 1, 1);
            }
            sbJson.Append("]");
            return sbJson.ToString();
        }
    }
}